home *** CD-ROM | disk | FTP | other *** search
- Path: mailhub.scitec.com.au!not-for-mail
- From: johns@rd.scitec.com.au (John Saunders)
- Newsgroups: comp.lang.c
- Subject: C syntax question
- Date: 11 Apr 1996 03:58:12 GMT
- Organization: SCITEC LIMITED, Sydney, Australia.
- Message-ID: <4ki00k$a4@mailhub.scitec.com.au>
- NNTP-Posting-Host: hydra.scitec.com.au
- X-Newsreader: TIN [UNIX 1.3 950515BETA PL0]
-
- I have a question on C syntax that doesn't seem to be covered by the books
- that I have. Maybe somebody with the full ANSI spec. could enlighten me.
-
- Consider the following code fragment:
-
- typedef unsigned char byte;
- typedef int data;
- typedef struct
- {
- byte byte;
- struct
- {
- int dummy;
- } data;
- } struct_t;
-
- This is supposedly correct C, typedef names are ignored when defining
- structure members. But not when defining variables so:
- byte byte;
- is an error when not inside a structure definition. Am I correct so far?
-
- I goal is to write a parser that handles the above correctly, all example
- ANSI C parsers I have seen don't. I started playing around with mixing
- typedefs and type keywords in the same declaration to see how C compilers
- react. This is where it got strange. Consider the code.
-
- typedef unsigned char ubyte;
- typedef signed char byte;
-
- ubyte signed i;
- byte unsigned j;
- signed ubyte k;
- unsigned byte l;
-
- The compilers I tried allowed the declaration of i and j (some gave warnings
- and others didn't). However none liked the declaration of k and l. From this
- it would seem that a typedef name is allowed only as the first token in the
- declaration. All the example ANSI C grammars that I have seen allow any number
- of typedef names or type keywords in any order. This is causing a major
- problem in being able to parse "byte byte;" correctly in the structure
- definition. I.e. is the second occurance of "byte" supposed to be treated as
- a typedef name or the member name?
-
- Has anyone tackled this problem before?
-
- Thanks.
- -- +------------------------------------------------------------+
- . | John Saunders - johns@rd.scitec.com.au (Work) |
- ,--_|\ | - john@nlc.net.au (Home) |
- / Oz \ | - http://www.nlc.net.au/~john/ |
- \_,--\_/ | SCITEC LIMITED - Phone +61 2 428 9563 - Fax +61 2 428 9933 |
- v +------------------------------------------------------------+
-